#include "MyString.h"

#include <string.h>
#include <stdio.h>

void InitMyString(MyStr *s)
{
    s->len = 0;  //将s结构体的len变量置0
    memset(s->str, 0, Size);//将str，置0，长度为si
}

void InsertStr(MyStr *s, MyStr *newStr)
{
    strncpy(s->str, newStr->str, Size - 1);
    s->str[Size - 1] = '\0'; 
    s->len = strlen(s->str);//测量str长度将值赋给len
}

void PrintStr(MyStr *s, bool HaveEnter)
{
    if (HaveEnter)
        printf("%s\n", s->str);
    else
        printf("%s", s->str);
}

void AppendStr(MyStr *s,const char *newStr)
{
    strncat(s->str,newStr,Size-1-s->len);
    s->len=strlen(s->str);
}
//接受一个指向MyStr结构的指针，和一个要移除的子字符串
void RemoveString(MyStr *s,const char *str)
{
    char *dest=strstr(s->str,str);//查找移除字串的出现位置
    while (dest !=NULL)
    {
        char *back=dest+strlen(str);//strlen计算字符串元素长度到’\0'结束
        while(*back !='\0')
            *dest++=*back++;
        *dest ='\0';
        dest =strstr(s->str,str);//检查str中还是否有子串，有继续运行，有则返回NULL，while中断
    }
    s->len=strlen(s->str); //更行长度 
}

char *GetSPtr(MyStr *s)//返回结构体中字符串数组的指针
{
    return s->str;
}

void GetStr(MyStr *s)
{
    char c=0;
    //读到回车结束
    while ((c=getchar())!='\n'&&s->len<Size-1)
    {
        s->str[s->len++]=c;
    }
    s->str[s->len] = '\0';
}

bool IsStrEqual(MyStr *s,MyStr *str)
{
    if(strcmp(s->str,str->str)==0)
        return true;
    else
        return false;
}

